package org.liang.auth.controller;

import io.swagger.annotations.ApiOperation;
import org.liang.auth.domain.Permission;
import org.liang.auth.dto.LoginDTO;
import org.liang.auth.service.IMenuService;
import org.liang.auth.domain.Menu;
import org.liang.auth.query.MenuQuery;
import org.liang.base.annotation.RonghuaPermission;
import org.liang.base.utils.AjaxResult;
import org.liang.base.utils.PageInfo;
import org.liang.system.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    public IMenuService menuService;

    /**
     * 保存和修改公用的
     * @param menu  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult save(@RequestBody Menu menu){
        try {
            if( menu.getId()!=null)
                menuService.update(menu);
            else
                menuService.insert(menu);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Serializable id){
        try {
            menuService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
    * 删除所有对象信息
    * @param ids
    * @return
    */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Serializable> ids){
        try {
            menuService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id")Serializable id)
    {
        try {
            Menu menu = menuService.selectById(id);
            return AjaxResult.success(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult selectAll(){
        try {
            List< Menu> menus = menuService.selectAll();
            return AjaxResult.success(menus);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult queryPage(@RequestBody MenuQuery query)
    {
        try {
            PageInfo<Menu> pageInfo = menuService.queryPage(query);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 根据用户id  查询对应菜单
     * @param id  传递的参数
     * @return Ajaxresult转换结果
     */
    @PutMapping("/{id}")
    public AjaxResult queryMenu(@PathVariable Long id){
        try {
            List<Menu> tree = menuService.queryMenuTree(id);
            return AjaxResult.success(tree);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询菜单树失败!"+e.getMessage());
        }
    }

}
